﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EasyStep.Business.Interfaces;
using EasyStep.Business.Providers;
using EasyStep.Data.Entities;
using EasyStep.Business.Interfaces.Providers;

namespace EasyStep.Business.Managers
{
    public class TestConstantsManager : BaseDataManager<ITestConstantsProvider, TestConst>, ITestConstantManager
    {
        public TestConstantsManager(ITestConstantsProvider provider) : base(provider){}


        public IEnumerable<TestConst> GetConstants(Guid listId)
        {
            return Provider.GetConstants(listId);
        }     

         
        public TestConst GetOrCreateConst(string name,string value, Guid constantListId, Func<TestConst> onNotFound = null)
        {            
            if (onNotFound==null)
                onNotFound=()=>new TestConst() { Name = name, Value=value, ListId = constantListId };

            TestConst result = null;
            //by id 
            Guid prId;
            if (Guid.TryParse(name, out prId) && !prId.Equals(Guid.Empty))
                result = Get(prId);
            if (result != null || string.IsNullOrEmpty(name)) return result;
            //by name
            result = GetOrCreateItemByName(name, () => GetConstants(constantListId), onNotFound);
            return result;
        } 

        public TestConst Get(Guid id)
        {
            return Provider.Get(id);
        }


        public TestConst Get(string name)
        {
            throw new NotImplementedException();
        }


        public string Convert(TestConst item)
        {
            return item==null? null:item.Serialize();
        }

        public TestConst Convert(string item)
        {
            return item.Deserialize<TestConst>();
        }


        public IEnumerable<TestConst> GetAll()
        {
            throw new NotImplementedException();
        }


        public IEnumerable<TestConst> UpdateAll(IEnumerable<TestConst> toData)
        {
            throw new NotImplementedException();
        }
    }


    public class TestConstantsListManager : BaseDataManager<ITestConstantsListProvider, TestConstList>, ITestConstantsListManager
    {
        public TestConstantsListManager(ITestConstantsListProvider provider): base(provider){}

        public IEnumerable<TestConstList> GetProjectConstList(Guid projectId)
        {
           return Provider.GetProjectConstList(projectId);
        }



        public TestConstList GetOrCreateConstList(string name, Guid projectId, Func<TestConstList> onNotFound = null)        
        {
            if (onNotFound == null)
                onNotFound = () => new TestConstList() { Name = name, ProjectId= projectId};

            TestConstList result = null;
            //by id 
            Guid prId;
            if (Guid.TryParse(name, out prId) && !prId.Equals(Guid.Empty))
                result = Get(prId);
            if (result != null || string.IsNullOrEmpty(name)) return result;
            //by name
            result = GetOrCreateItemByName(name, () => GetProjectConstList(projectId), onNotFound);
            return result;
        }

        public TestConstList Get(Guid id)
        {
            return Provider.Get(id);
        }


        
    }

}